package com.zjh.photoshopstudy.app.course;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zjh.photoshopstudy.Model.VideoEntity;
import com.zjh.photoshopstudy.R;
import com.zjh.photoshopstudy.app.collection.CollectPresent;
import com.zjh.photoshopstudy.app.collection.CollectionAdapter;
import com.zjh.photoshopstudy.app.history.onItemCheck;

import java.util.ArrayList;
import java.util.List;

public class CourseActivity extends AppCompatActivity implements onItemCheck<VideoEntity> {
    CoursePresent mPresent;
    CollectionAdapter adapter;
    TextView selectBtn;
    TextView tip;
    LinearLayout layout;
    boolean isSelect = false;
    boolean isAllSelect = false;
    List<VideoEntity> collectionList = new ArrayList<>();
    RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course);
        mPresent = new CoursePresent(this);
        initView();
    }

    private void initView() {
        recyclerView = findViewById(R.id.courseList);
        layout = findViewById(R.id.layout);
        tip = findViewById(R.id.no_history_tip3);
        selectBtn = findViewById(R.id.textView16);
        mPresent.getCourseList();
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancel:
                doCancel();
                break;
            case R.id.delete:
                doDelete();
                break;
            case R.id.textView16:
                clickSelectBtn();
                break;
        }
    }

    private void doDelete() {
        mPresent.deleteCollect(collectionList);
    }

    private void doCancel() {
        layout.setVisibility(View.INVISIBLE);
        isSelect = false;
        adapter.setSelect(false);
        collectionList.clear();
        adapter.notifyDataSetChanged();
        selectBtn.setText("管理");
    }

    @Override
    public void onItemCheck(VideoEntity entity) {
        collectionList.add(entity);
        if (collectionList.size() == adapter.getItemCount()) {
            isAllSelect = true;
            selectBtn.setText("全不选");
        }
    }

    @Override
    public void onItemCancelCheck(VideoEntity entity) {
        collectionList.remove(entity);
        if (collectionList.size() != adapter.getItemCount()) {
            isAllSelect = false;
            if (isSelect) {
                selectBtn.setText("全选");
            } else {
                selectBtn.setText("管理");
            }

        }
    }

    private void clickSelectBtn() {
        if (isSelect) {
            collectionList.clear();
            if (isAllSelect) {
                adapter.setAllSelect(false);
                selectBtn.setText("全选");
            } else {
                adapter.setAllSelect(true);
                selectBtn.setText("全不选");
            }
            isAllSelect = !isAllSelect;
            adapter.notifyDataSetChanged();
        } else {
            adapter.setSelect(true);
            adapter.notifyDataSetChanged();
            layout.setVisibility(View.VISIBLE);
            selectBtn.setText("全选");
            isSelect = true;
        }
    }

    public void onDeleteCollectSuccess() {
        List<VideoEntity> list = adapter.getList();
        list.removeAll(collectionList);
        adapter.setList(list);
        adapter.notifyDataSetChanged();
        if (adapter.getItemCount() == 0) {
            selectBtn.setVisibility(View.INVISIBLE);
            tip.setVisibility(View.VISIBLE);
        }
        doCancel();
    }

    public void onDeleteCollectFail() {
        Toast.makeText(this, "刪除失败", Toast.LENGTH_SHORT).show();
    }

    public void OnGetCollectListSuccess(List<VideoEntity> collectionList) {
        adapter = new CollectionAdapter(collectionList, this, this);
        if (collectionList != null && collectionList.size() > 0) {
            recyclerView.setAdapter(adapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
        } else {
            tip.setVisibility(View.VISIBLE);
            selectBtn.setVisibility(View.INVISIBLE);
        }
    }

    public void OnGetCollectListFail() {
        Toast.makeText(this, "获取收藏失败", Toast.LENGTH_SHORT).show();
    }

}
